import os.path

import requests

from APis.baseAPI import BaseAPI
from common.conf import data_path

from common.config import ip
from common.dbDB import DoDB


class LogAPI(BaseAPI):
    header = {'Admin-Token':''}
    def findData(self,fname):
        fData = {'type': 'img'}
        # fname = r'C:\python\2504\apiTest\datas\123.jpg'
        fName=os.path.join(data_path,fname)
        fData2 = {'file': (fname, open(fName, 'rb'), 'image/jpeg')}
        r2 = requests.post(f'{ip}/file/upload', data=fData, files=fData2, headers=self.header)
        ebatchId = r2.json()['batchId']
        return ebatchId

    # def addLog(self,ebatchId):
    #     getLogData = {"categoryId": "1", "content": "123", "tomorrow": "132", "question": "321", "batchId": ebatchId,
    #                   "sendUserIds": "", "sendDeptIds": "", "customerIds": "", "contactsIds": "", "businessIds": "",
    #                   "contractIds": ""}
    #     r3 = requests.post(f'{ip}/OaLog/addOrUpdate', json=getLogData, headers=self.header)
    #
    #     return r3.json()

    def addLog(self,d):
        r3 = requests.post(f'{ip}/OaLog/addOrUpdate', json=d, headers=self.header)

        return r3

    def getLog(self,d={'page': 1, 'limit': 5, 'by': 1}):
        # getLogData = {'page': 1, 'limit': 5, 'by': 1}

        # r = requests.post(f'{ip}/OaLog/queryList', json=getLogData, headers=self.header)
        r = requests.post(f'{ip}/OaLog/queryList', json=d, headers=self.header)
        # b_aId = r.json()['data']['list'][0]['logId']

        # return r,b_aId
        return r


    # def updateLog(self,b_aId,ebatchId):
    def updateLog(self,d):
        # updateLogData = {"logId": b_aId, "categoryId": "1", "content": "更改内容1", "tomorrow": "更改内容2",
        #                  "question": "更改内容3", "batchId": ebatchId, "sendUserIds": "", "sendDeptIds": "",
        #                  "customerIds": "", "contactsIds": "", "businessIds": "", "contractIds": ""}

        # r4 = requests.post(f'{ip}/OaLog/addOrUpdate', json=updateLogData, headers=self.header)
        r4 = requests.post(f'{ip}/OaLog/addOrUpdate', json=d, headers=self.header)
        return r4

    def delLog(self,d):
        # delLogData = {"logId": b_aId}

        r5 = requests.post(f'{ip}/OaLog/deleteById', data=d, headers=self.header)
        return r5

    def getlogId(self):
        l=self.getLog().json()['data']['list']
        ll=[]
        for i in l:
            ll.append(i['logId'])
        return ll

    def getlogidsBySQL(self,sql):
        db = DoDB()
        s=db.doselectSQL(sql)
        ll=[]
        for i in s:
            ll.append(i[0])
        return ll


    def qinfchu(self):
        l=self.getLog().json()['data']['list']
        for i in l:
            a = {'logId':i['logId']}
            self.delLog(a)

if __name__ == '__main__':
    # from APis.loginAPI import LoginAPI
    # r = LoginAPI().login()
    # token = r.json()['Admin-Token']
    #
    # lapi = LogAPI(token)
    #
    # bid=lapi.findData()
    # lapi.addLog(bid)

    log=LogAPI()
    sql = 'select log_id from 72crm_oa_log where create_user_id = (select user_id from  72crm_admin_user where username = "admin");'
    r=log.getlogidsBySQL(sql)
    print(r)
